import { observable, action, toJS } from 'mobx';
import { WeaForm, WeaTableNew } from 'comsMobx';
import { WeaTab, WeaLocaleProvider } from 'ecCom';
import * as smsRadioApi from '../../apis/smsRadio';
import { message } from 'antd';

const { TableStore } = WeaTableNew;
const getLabel = WeaLocaleProvider.getLabel;

export default class SmsRadioStore {
  @observable loading = false;
  @observable logTableStore = new TableStore();
  @observable receiverTableStore = new TableStore();
  @observable logSearchAdvance = {
    form: new WeaForm(),
    visible: false,
  }
  @observable receiverSearchAdvance = {
    form: new WeaForm(),
    visible: false,
  }
  @observable sendBroadcastForm = {
    form: new WeaForm(),
    visible: false,
  }
  @observable dataKey = '';
  @observable selectKey = '0';
  @observable logParams = {
    timeType: '',
    title: '',
    detail: '',
    receiver: '',
    fromDate: '',
    toDate: '',
  }
  @observable changedatas = {};
  @observable submitdatas = {};

  @observable workFlowReplaceData = [];
  @observable docReplaceData = [];
  @observable fileData = [];

  @observable conditionInfo =
    // {
    //   items:
    //   [
    // {
    //   browserConditionParam: {
    //     completeParams: {},
    //     conditionDataParams: {},
    //     dataParams: {},
    //     destDataParams: {},
    //     hasAddBtn: false,
    //     hasAdvanceSerach: true,
    //     idSeparator: ",",
    //     isAutoComplete: 1,
    //     isDetail: 0,
    //     isSingle: false,
    //     pageSize: 10,
    //     quickSearchName: "",
    //     replaceDatas: [],
    //     title: "广播类型",
    //     type: "radioMsgType",
    //     viewAttr: 3,
    //     rules: "required",
    // },
    //   colSpan: 2,
    //   conditionType: "BROWSER",
    //   rules: "required",
    //   domkey: ["radioType"],
    //   isQuickSearch: false,
    //   label: "广播类型",
    //   viewAttr: 3
    // },
    // {
    //   colSpan: 2,
    //   rules: "selectLinkageRequired",
    //   conditionType: "SELECT_LINKAGE",
    //   selectLinkageDatas: {
    //     "1": {
    //       conditionType: "BROWSER",
    //       domkey: ["INPUT"]
    //     },
    //   },
    //   options: [
    //     { key: "0", showname: "全部", selected: true },
    //     { key: "1", showname: "指定人" },
    //   ],
    //   domkey: ["radioPerson"],
    //   isQuickSearch: false,
    //   label: "广播接收人",
    //   viewAttr: 3
    // },
    {
      colSpan: 1,
      conditionType: 'INPUT',
      rules: 'required',
      domkey: ['radioTitle'],
      isQuickSearch: true,
      label: getLabel(514515, '广播标题'),
      value: '',
      viewAttr: 3,
      length: 150,
    };

  @observable conditioninfoAdvanceTime = [];
  @observable dialogVisible = false;
  @observable receiverDialogVisible = false;
  @observable msgDialogVisible = false;
  @observable tipDialogVisible = false;
  @observable checkLogItem = {};

  @observable workFlowBiz = '';
  @observable docBiz = '';
  @observable fileBiz = '';

  @observable msgDialogItem = {};
  @observable receiverIds = {};
  @observable receiverParams = {
    receiverid: '',
  };

  @observable sendRadioParams = {
    msgType: '',
    title: '',
    contexts: '',
    receiverid: '',
    biz: '',
  }
  // 消息中心调用显示dialog方法
  @action
  showMsgDialog = (id) => {
    this.loading = true;
    this.setState({
      workFlowBiz: '',
      docBiz: '',
      fileBiz: '',
    });
    smsRadioApi.getMsgBroadcastDialog({ broadcastid: id }).then((res) => {
      if (res.status) {
        this.loading = false;
        this.msgDialogItem = res;
        if (res.workFlowBiz) {
          this.workFlowBiz = res.workFlowBiz.workFlowBizList;
        }
        if (res.docBiz) {
          this.docBiz = res.docBiz.docBizList;
        }
        if (res.fileBiz) {
          this.fileBiz = res.fileBiz.fileBizList;
        }
        this.msgDialogVisible = true;
      }
    });
  }
  // 消息中心关闭dialog方法
  @action
  closeMsgDialog = () => {
    this.setState({
      workFlowBiz: '',
      docBiz: '',
      fileBiz: '',
      msgDialogItem: {},
    });
    this.msgDialogVisible = false;
  }
  // pc单页查询广播详情
  @action
  getPcRadioDetail = (id) => {
    this.loading = true;
    this.setState({
      workFlowBiz: '',
      docBiz: '',
      fileBiz: '',
      msgDialogItem: {},
    });
    smsRadioApi.getMsgBroadcastDialog({ broadcastid: id }).then((res) => {
      if (res.status) {
        this.loading = false;
        this.msgDialogItem = res;
        if (res.workFlowBiz) {
          this.workFlowBiz = res.workFlowBiz.workFlowBizList;
        }
        if (res.docBiz) {
          this.docBiz = res.docBiz.docBizList;
        }
        if (res.fileBiz) {
          this.fileBiz = res.fileBiz.fileBizList;
        }
      }
    });
  }
  // 广播消息发送记录-列表
  @action
  getSendLogTable = (isOnChange = false) => {
    let params = this.logSearchAdvance.form && this.logSearchAdvance.form.getFormParams() || {};
    this.loading = true;
    smsRadioApi.queryBroadcastRecord(params).then((res) => {
      if (res.datas) {
        this.loading = false;
        isOnChange
          ? this.logTableStore.getDatas(res.datas, 1)
          : this.logTableStore.getDatas(res.datas);
      }
      this.dataKey = res.datas;
    });
  }
  // 广播消息发送记录-高级搜索
  @action
  getLogCondition = () => {
    this.loading = true;
    smsRadioApi.getConditions().then((res) => {
      if (res.status) {
        this.loading = false;
        this.setConditionTimeAdvance();
        res.condition[0].items.push(this.conditioninfoAdvanceTime);
        this.logSearchAdvance.form.initFormFields(res.condition);
        this.logSearchAdvance.form.setCondition(res.condition);
      }
    },
    );
  }
  // 获取发送广播页面的两个item（广播类型，广播接收人）
  @action
  getSendBroadcastCondition = (msgtype) => {
    this.loading = true;
    smsRadioApi.getSendConditions({ msgtype }).then((res) => {
      if (res.status) {
        this.loading = false;
        res.condition[0].items.push(this.conditionInfo);
        this.sendBroadcastForm.form.initFormFields(res.condition);
        this.sendBroadcastForm.form.setCondition(res.condition);
      }
    });
  }

  // 点击标题显示广播详情
  @action
  getBroadcastContext = (id) => {
    this.loading = true;
    this.setState({
      workFlowBiz: '',
      docBiz: '',
      fileBiz: '',
      workFlowReplaceData: [],
      docReplaceData: [],
      fileData: [],
      changedatas: {},
    });
    smsRadioApi.getBroadcastContexts({ id }).then((res) => {
      if (res.status) {
        this.loading = false;
        this.checkLogItem = res;
        if (res.workFlowBiz) {
          this.workFlowBiz = res.workFlowBiz.workFlowBizList;
          this.workFlowReplaceData = res.workFlowJson;
          this.changedatas.signworkflowids = res.workFlowBizids;
          this.changedatas.signworkflowreplacedata = res.workFlowJson;
        }
        if (res.docBiz) {
          this.docBiz = res.docBiz.docBizList;
          this.docReplaceData = res.docJson;
          this.changedatas.signdocids = res.docBizids;
          this.changedatas.signdocreplacedata = res.docJson;
        }
        if (res.fileBiz) {
          this.fileBiz = res.fileBiz.fileBizList;
          this.fileData = res.fileJson;
          this.changedatas.uploadfileids = res.fileBizids;
          this.changedatas.uploadfiledata = res.fileJson;
        }
        this.dialogVisible = true;
      }
    });
  }

  // 点击右侧重新发送查询广播内容
  @action
  getOperatesContext = (id) => {
    this.loading = true;
    this.setState({
      workFlowBiz: '',
      docBiz: '',
      fileBiz: '',
      workFlowReplaceData: [],
      docReplaceData: [],
      fileData: [],
      changedatas: {},
    });
    return smsRadioApi.getBroadcastContexts({ id }).then((res) => {
      if (res.status) {
        this.loading = false;
        this.checkLogItem = res;
        if (res.workFlowBiz) {
          this.workFlowBiz = res.workFlowBiz.workFlowBizList;
          this.workFlowReplaceData = res.workFlowJson;
          this.changedatas.signworkflowids = res.workFlowBizids;
          this.changedatas.signworkflowreplacedata = res.workFlowJson;
        }
        if (res.docBiz) {
          this.docBiz = res.docBiz.docBizList;
          this.docReplaceData = res.docJson;
          this.changedatas.signdocids = res.docBizids;
          this.changedatas.signdocreplacedata = res.docJson;
        }
        if (res.fileBiz) {
          this.fileBiz = res.fileBiz.fileBizList;
          this.fileData = res.fileJson;
          this.changedatas.uploadfileids = res.fileBizids;
          this.changedatas.uploadfiledata = res.fileJson;
        }
      }
    });
  }


  // 查询广播消息接收人-列表
  @action
  getReceiverTable = () => {
    this.receiverParams = this.receiverSearchAdvance.form && this.receiverSearchAdvance.form.getFormParams() || {};
    this.receiverParams.receiverid = this.receiverIds.receiverid;
    this.loading = true;
    smsRadioApi.getReceiverList(this.receiverParams).then((res) => {
      this.loading = false;
      if (res.datas) {
        this.receiverTableStore.getDatas(res.datas, 1);
      }
      this.dataKey = res.datas;
    });
  }

  // 广播消息接收人-高级搜索
  @action
  getRadioReceiverCondition = () => {
    this.loading = true;
    smsRadioApi.getReceiverConditions().then((res) => {
      if (res.status) {
        this.loading = false;
        this.receiverSearchAdvance.form.initFormFields(res.condition);
        this.receiverSearchAdvance.form.setCondition(res.condition);
      }
    },
    );
  }

  // 发送广播消息
  @action
  sendRadioMsg = (context) => {
    this.loading = true;
    if (this.getFormParams().receiver.value[0] == 0) { // 广播接收人选了全部
      this.sendRadioParams.receiverid = -1;
    }
    if (this.getFormParams().receiver.value[0] == 1) { // 广播接收人选了指定人，则使用浏览按钮选中的userid
      this.sendRadioParams.receiverid = this.getFormParams().receiver.value[1][0];//
    }
    this.sendRadioParams.contexts = context;
    this.sendRadioParams.msgType = this.getFormParams().type.value;
    this.sendRadioParams.title = this.getFormParams().radioTitle.value;
    this.sendRadioParams.biz = JSON.stringify(this.changedatas);
    smsRadioApi.sendBroadcast(this.sendRadioParams).then((res) => {
      if (res.status) {
        this.loading = false;
        this.getSendLogTable(); // 重新获取新的发送记录
        message.success(getLabel(27564, '发送成功'));
        this.setState({
          selectKey: '1',
          changedatas: {},
          fileData: [],
          docReplaceData: [],
          workFlowReplaceData: [],
        });
        this.getSendBroadcastCondition();
        // setTimeout(function () {
        //   window.location.href=`/spa/smallApp/static/index.html#/main/sms/radioDetail?id=${res.broadcastid}`;
        // }, 2000);
      }
    });
  }

  // 赋值方法
  @action
  setState = (params = {}) => {
    Object.keys(params).forEach((key) => {
      this[key] = params[key];
    });
  }

  // 获取发送广播页面参数
  @action
  getFormParams() {
    return this.sendBroadcastForm.form.getFormDatas();
  }

  // 获取广播发送记录高级搜索 时间条目
  @action
  setConditionTimeAdvance() {
    this.conditioninfoAdvanceTime =
      {
        colSpan: 2,
        conditionType: 'SELECT_LINKAGE',
        label: getLabel(18961, '发送时间'),
        options: [
          { key: 'allDay', showname: getLabel(332, '全部'), selected: this.selectedKey === 'allDay' },
          { key: 'today', showname: getLabel(15537, '今天'), selected: this.selectedKey === 'today' },
          { key: 'week', showname: getLabel(15539, '本周'), selected: this.selectedKey === 'week' },
          { key: 'month', showname: getLabel(15541, '本月'), selected: this.selectedKey === 'month' },
          { key: 'lastMonth', showname: getLabel(27347, '上个月') },
          { key: 'quarter', showname: getLabel(21904, '本季'), selected: this.selectedKey === 'quarter' },
          { key: 'year', showname: getLabel(15384, '本年'), selected: this.selectedKey === 'year' },
          { key: 'lastYear', showname: getLabel(81716, '上一年') },
          { key: 'specifiedDate', showname: getLabel(32530, '指定日期范围') },
        ],
        domkey: ['timeType'],
        selectLinkageDatas: {
          specifiedDate: {
            conditionType: 'RANGEPICKER',
            domkey: ['fromDate', 'toDate'],
          },
        },
        labelcol: 6,
        fieldcol: 18,
      };
  }
}
